package com.heatup.apt.management.service.company.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;
import com.heatup.apt.common.dao.CompanyInfoMapper;
import com.heatup.apt.common.dao.CompanyRechargeMapper;
import com.heatup.apt.common.dao.OrderInfoMapper;
import com.heatup.apt.management.service.company.CompanyRechargeService;
import com.heatup.apt.model.company.CompanyInfo;
import com.heatup.apt.model.company.CompanyRecharge;
import com.heatup.apt.model.order.OrderCompanyDetail;
import com.heatup.apt.model.order.OrderCompanyInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * CompanyRecharge 表数据服务层接口实现类
 */
@Service
public class CompanyRechargeServiceImpl extends SuperServiceImpl<CompanyRechargeMapper, CompanyRecharge> implements CompanyRechargeService {

    @Autowired
    CompanyRechargeMapper companyRechargeMapper;
    @Autowired
    CompanyInfoMapper companyInfoMapper;
    @Autowired
    OrderInfoMapper orderInfoMapper;

    /**
     * 更新企业余额 及保存企业充值记录
     *
     * @param companyRecharge 企业充值
     * @param userId          后台操作用户ID
     * @return true成功 / false失败
     */
    @Override
    public boolean saveCompanyRecharge(CompanyRecharge companyRecharge, Integer userId) {
        if (companyRecharge == null || companyRecharge.getCompanyId() == null)
            return false;

        CompanyInfo companyInfo = companyInfoMapper.selectById(companyRecharge.getCompanyId());
        if (companyInfo == null)
            return false;

        companyRecharge.setBeforeTransferAmount(companyInfo.getAmount());

        companyInfo.setId(companyRecharge.getCompanyId());
        BigDecimal amount = companyInfo.getAmount() == null ? new BigDecimal(0) : companyInfo.getAmount();
        amount = amount.add(companyRecharge.getAmount());
        companyInfo.setAmount(amount);

        companyRecharge.setAfterTransferAmount(companyInfo.getAmount());
        companyRecharge.setCreatedBy(Long.valueOf(userId));
        companyRecharge.setCreatedTime(new Date());
        boolean updateCompanyAmount = companyInfoMapper.updateSelectiveById(companyInfo) > 0;
        if (!updateCompanyAmount)
            throw new RuntimeException("更新充值金额失败");
        return companyRechargeMapper.insertSelective(companyRecharge) > 0;
    }

    /**
     * 企业历史充值流水 分页
     *
     * @param companyRechargePage 企业充值流水分页类
     * @param year
     * @param month               月份
     * @param companyId           企业ID   @return 企业历史充值流水
     */
    @Override
    public Page<CompanyRecharge> selectRechargePage(Page<CompanyRecharge> companyRechargePage, Integer year, Integer month, Long companyId) {
        companyRechargePage.setRecords(companyRechargeMapper.selectCompanyRechargeHistoryList(companyRechargePage, year, month, companyId));
        return companyRechargePage;
    }

    /**
     * 企业储值卡消费流水
     *
     * @param pageIndex        页码
     * @param pageSize         页面大小
     * @param orderCompanyInfo 查询条件
     * @return 企业消费流水
     */
    @Override
    public Map<String, Object> queryCompanyCostList(Integer pageIndex, Integer pageSize, OrderCompanyInfo orderCompanyInfo) {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("pageIndex", (pageIndex - 1) * pageSize);
        queryMap.put("pageSize", pageSize);
        queryMap.put("companyId", orderCompanyInfo.getCompanyId());
        queryMap.put("year", orderCompanyInfo.getYear());
        queryMap.put("month", orderCompanyInfo.getMonth());
        resultMap.put("list", orderInfoMapper.queryCompanyCostList(queryMap));
        resultMap.put("count", orderInfoMapper.queryCompanyCostCount(queryMap));
        return resultMap;
    }

    /**
     * 企业储值卡已完成订单列表
     *
     * @param page               分页插件
     * @param orderCompanyDetail 查询类
     * @return 企业已完成订单列表
     */
    @Override
    public Page<OrderCompanyDetail> queryOrderPage(Page<OrderCompanyDetail> page, OrderCompanyDetail orderCompanyDetail) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("name", orderCompanyDetail.getName());
        queryMap.put("status", orderCompanyDetail.getOrderStatus());
        queryMap.put("mobile", orderCompanyDetail.getMobile());
        queryMap.put("companyId", orderCompanyDetail.getCompanyId());
        queryMap.put("year", orderCompanyDetail.getYear());
        queryMap.put("month", orderCompanyDetail.getMonth());
        queryMap.put("orderStatus", orderCompanyDetail.getOrderStatus());
        queryMap.put("orderNumber", orderCompanyDetail.getOrderNumber());
        page.setRecords(orderInfoMapper.queryCompanyOrderList(page, queryMap));
        return page;
    }


}